# -*- coding: utf-8 -*-
import matplotlib.font_manager
import pymysql
import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
import matplotlib.pyplot as plt
import numpy as np

ALL_DATA = []

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

headers = {
    "User-Agent": UserAgent().random
    # "User-Agent": "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)"
}

connect = pymysql.connect(host="localhost", user="zengchen", password="zengchen233",
                          database="big_data", cursorclass=pymysql.cursors.DictCursor)


def send_parse_urls(start_urls):
    global city, temp
    for start_url in start_urls:
        response = requests.get(start_url, headers)
        # 编码问题的解决
        response = response.text.encode("raw_unicode_escape").decode("utf-8")
        soup = BeautifulSoup(response, "html5lib")  # LxmZ 解析器:性能比较好，html5lib : 适合页面结
        div_tall = soup.find("div", "conMidtab")  # find() 找符合要求的第一个元素
        tables = div_tall.find_all("table")  # find_all() 找到符合要求的所有元素的列表
        for table in tables:
            trs = table.find_all("tr")
            info_trs = trs[2:]
            for index, info_tr in enumerate(info_trs):
                city_td = info_tr.find_all("td")[0]
                temp_td = info_tr.find_all("td")[6]
                if index == 0:
                    city_td = info_tr.find_all("td")[0]
                    temp_td = info_tr.find_all("td")[7]
                city = list(city_td.stripped_strings)[0]
                temp = list(temp_td.stripped_strings)[0]
            ALL_DATA.append({"city": city, "temp": int(temp)})
            try:
                # 插入数据
                with connect.cursor() as cursor:
                    print(city, temp)
                    sql = "insert into city_temp values ('" + city + "','" + temp + "');"
                    # print(sql)
                    cursor.execute(sql)
                    connect.commit()
                    print("插入成功！")
            except pymysql.err as e:
                print("Mysql Error %d: %s" % (e.args[0], e.args[1]))
            finally:
                cursor.close()
                # connect.close()
    return ALL_DATA


def get_urls():
    urls = ["http://www.weather.com.cn/textFC/hb.shtml",
            "http://www.weather.com.cn/textFC/db.shtml",
            "http://www.weather.com.cn/textFC/hd.shtml",
            "http://www.weather.com.cn/textFC/hz.shtml",
            "http://www.weather.com.cn/textFC/hn.shtml",
            "http://www.weather.com.cn/textFC/xb.shtml",
            "http://www.weather.com.cn/textFC/xn.shtml",
            "http://www.weather.com.cn/textFC/gat.shtml"
            ]
    return urls


def gen_html():
    with open("temperature.html", 'w') as png:
        html = '<img src=\"{}\"/>'.format("img/low-temperature.png")
        png.write(html)


def main():
    start_urls = get_urls()
    data = send_parse_urls(start_urls)
    connect.close()
    # print(data)
    data.sort(key=lambda data: data["temp"])
    show_data = data[:10]
    city = list(map(lambda data: data["city"], show_data))
    temp = list(map(lambda data: data["temp"], show_data))

    temp = np.array(temp).astype(dtype=int).tolist()

    font = matplotlib.font_manager.FontProperties(fname="font/SourceHanSansSC-Regular.otf")
    font1 = {'color': 'red', 'size': 20}
    print(city, temp)

    x = np.array(city)
    y = np.array(temp)
    color = ["#99CC33", "#FF9900", "#FFCC00", "#666699", "#FF6666", "#339999", "#339933", "#99CCFF", "#000000",
             "#99CC99"]
    plt.title("全国最低气温", fontproperties=font, fontdict=font1)
    plt.xlabel("城市", fontproperties=font, fontdict=font1)
    plt.ylabel("温度", fontproperties=font, fontdict=font1)
    plt.bar(x, y, color=color, bottom=0)
    plt.savefig('img/low-temperature.png')
    gen_html()


if __name__ == '__main__':
    # 删除数据
    with connect.cursor() as cursor:
        sql = "truncate table city_temp;"  # 删除之前数据库的所有数据
        cursor.execute(sql)
        connect.commit()
    main()
